package com.grts.mall.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grts.mall.common.CommonResult;
import com.grts.mall.common.ResultForLayuiTable;
import com.grts.mall.pojo.Category;
import com.grts.mall.service.CategoryService;

@Controller
public class CategoryController {
	
	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping("/category/manage")
	public String list() {
		return "category/manage";
	}
	
	@RequestMapping("/category/list")
	@ResponseBody
	public ResultForLayuiTable listJson(int page, int limit) {
		long count = this.categoryService.count();
		List<Category> data = this.categoryService.findPageList(page, limit);
		return new ResultForLayuiTable(0, "OK", data, count);
	}
	
	@RequestMapping("/category/add")
	public String add() {
		return "category/add";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public CommonResult save(Category category) {
		this.categoryService.save(category);
		return CommonResult.success();
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public CommonResult del(Long cid) {
		this.categoryService.del(cid);
		return CommonResult.success();
	}
	
	@RequestMapping("/edit")
	public String edit(Long cid,Model model) {
		Category category = this.categoryService.findById(cid);
		model.addAttribute("category", category);
		return "category/edit";
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public CommonResult update(Category category) {
		this.categoryService.update(category);
		return CommonResult.success();
	}
	
	@RequestMapping("/group/del")
	@ResponseBody
	public CommonResult groupdel(@RequestParam("cid[]") List<Long> cid) {
		this.categoryService.groupDel(cid);
		return CommonResult.success();
	}
}
